﻿using MediatR;
using Newtonsoft.Json;
using System.ComponentModel.DataAnnotations;
using Warehouse.Domain.DTO;
using Warehouse.Infrstructre.Interface;
using Warehouse.Read.API.Applcation.Command.OutBoundCommand;
using static WMSpublic.ParseAPI;
using WMSpublic;
namespace Warehouse.Read.API.Applcation.CommandHandlers.OutBoundCommandHandlers
{
    public class FillOutShipCommandHandlers:IRequestHandler<FillOutShipCommand, List<OutShipShowDTO>>
    {

        private readonly IOutShipRepository _outShipRepository;

        private readonly IOutProductRepository _outproductRepository;
        private readonly IStorehouseRepository _storehouse;

        public FillOutShipCommandHandlers(IOutShipRepository outShipRepository, IOutProductRepository outproductRepository, IStorehouseRepository storehouse)
        {
            _outShipRepository = outShipRepository;
            _outproductRepository = outproductRepository;
            _storehouse = storehouse;
        }

        public async Task<List<OutShipShowDTO>> Handle(FillOutShipCommand request, CancellationToken cancellationToken)
        {
            List<Products> products = await GetProductsList(1, 100);
            List<Client> clients = await GetClientList(1, 100);

            var list = (from a in (await _outShipRepository.GetAllAsyns())
                        join b in await _outproductRepository.GetAllAsyns() on a.OutShipId equals b.OutShipId
                        join c in products on b.ProductsID equals c.ProductsID
                        join d in clients on a.ClientId equals d.ClientId
                        join e in await _storehouse.GetAllAsyns() on c.StorehouseId equals e.StorehouseId
                        where a.OutShipId == request.OutShipId && a.IsDel == false
                        select new OutShipShowDTO
                        {
                            OutShipId = a.OutShipId,
                            OutShipCode = a.OutShipCode,
                            OutShipType = a.OutShipType,
                            MakePerson = a.MakePerson,
                            MarkTime = a.MarkTime,
                            ClientName = d.ClientName,
                            ClientCode = d.ClientCode,
                            Ccontact = d.Ccontact,
                            VendorPhone = d.Cphone,
                            Binlocation = e.StorehouseName,
                            ProductsName = c.ProductsName,
                            ProductsCode = c.ProductsCode,
                            OutProuductNum = b.OutProuductNum,
                            Specs = c.Specs,
                            Batch = b.Batch,
                            AuditStart = a.AuditStart,
                            AuditResult = a.AuditResult,
                            OrderNumber = a.OrderNumber,
                            OutPrice = b.OutPrice,
                            
                        });
            return list.ToList() ;
        }


        /// <summary>
        /// 产品
        /// </summary>
        /// <returns></returns>
        public async Task<dynamic> GetProductsList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMiwyIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWwj-e-jiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6IiIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.F6NVEvAxW0LXOqPcIA8hWoL54BSxdIQTFtrJ9r0Ah18";
            HttpClientWrapper cilent = new HttpClientWrapper(token);

            string chanpin = "http://10.223.8.21:8000/cpd/api/ProductRead/GetAllProductInfo?pageindex=" + pindex + "&pagesize=" + psize;

            string postReponse = await cilent.GetAsync(chanpin);
            var json = JsonConvert.DeserializeObject<ApiResultList<Products>>(postReponse);
            return json.data.list;
        }
        //客户
        public async Task<dynamic> GetClientList(int pindex, int psize)
        {
            string token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIyOCIsInNpZCI6IjE4NTk4MDM1MDY2MzA4NTY3MDQiLCJodHRwOi8vc2NoZW1hcy5taWNyb3NvZnQuY29tL3dzLzIwMDgvMDYvaWRlbnRpdHkvY2xhaW1zL2dyb3Vwc2lkIjoiMiwyIiwianRpIjoiNDMzMDY2ZGYtZjI2MC00MjY3LWJhZjktYmQzODFkZWFkYzBiIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvbmFtZSI6IuWwj-e-jiIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3VyaSI6IiIsImlzcyI6IlJlbmFvYm8yMDA0IiwiYXVkIjoiV2VpZnV3dTIyMDNBIn0.F6NVEvAxW0LXOqPcIA8hWoL54BSxdIQTFtrJ9r0Ah18";
            HttpClientWrapper cilent = new HttpClientWrapper(token);
            string kehu = "http://116.205.121.197:8000/ywd/api/ClientRead/GetClient?pageindex=" + pindex + "&pagesize=" + psize;
            string postReponse = await cilent.GetAsync(kehu);
            var json = JsonConvert.DeserializeObject<ApiResultList<Client>>(postReponse);
            return json.data.list;

        }

        //产品
        public class Products
        {
            /// <summary>
            /// 产品id
            /// </summary>
            public int ProductsID { get; set; }
            /// <summary>
            /// 产品编号
            /// </summary>
            public string? ProductsCode { get; set; }
            /// <summary>
            /// 产品名称
            /// </summary>
            public string? ProductsName { get; set; }
            /// <summary>
            /// 厂商编码
            /// </summary>
            public string? VendorsCode { get; set; }
            /// <summary>
            /// 内部编码
            /// </summary>
            public string? InteriorCode { get; set; }
            /// <summary>
            /// 规格
            /// </summary>
            public string? Specs { get; set; }
            /// <summary>
            /// 产品类别
            /// </summary>
            public int? ProductTypeID { get; set; }
            /// <summary>
            /// 存储单位
            /// </summary>
            public string? StorageUnits { get; set; }
            /// <summary>
            /// 预警（上）
            /// </summary>
            public int? Upperlimit { get; set; }
            /// <summary>
            /// 预警（下）
            /// </summary>
            public int? Lowerlimit { get; set; }
            /// <summary>
            /// 包装类型
            /// </summary>
            public int? WrapType { get; set; }
            /// <summary>
            /// 价格
            /// </summary>
            public decimal? Price { get; set; }
            /// <summary>
            /// 重量
            /// </summary>
            public decimal? Weight { get; set; }
            /// <summary>
            /// 默认仓库
            /// </summary>
            public int? WarehouseId { get; set; }
            /// <summary>
            /// 库位
            /// </summary>
            public int? StorehouseId { get; set; }
            /// <summary>
            /// 默认供应商
            /// </summary>
            public int? VendorId { get; set; }
            /// <summary>
            /// 默认客户
            /// </summary>
            public int? ClientId { get; set; }
            /// <summary>
            /// 描述
            /// </summary>
            public string? Describe { get; set; }
            /// <summary>
            /// 产品数量
            /// </summary>
            public int? ProcurementNum { get; set; }
            /// <summary>
            /// 公司id
            /// </summary>
            public string? Office_Id { get; set; }

            /// <summary>
            /// 用户id
            /// </summary>
            public int? User_Id { get; set; }
        }

        //客户
        public class Client
        {
            /// <summary>
            /// 客户id
            /// </summary>
            [Key]
            public int ClientId { get; set; }
            /// <summary>
            /// 客户编号
            /// </summary>
            public string? ClientCode { get; set; }
            /// <summary>
            /// 客户名称
            /// </summary>
            public string? ClientName { get; set; }
            /// <summary>
            /// 联系人
            /// </summary>
            public string? Ccontact { get; set; }
            /// <summary>
            /// 传真
            /// </summary>
            public string? Cfax { get; set; }
            /// <summary>
            /// 电话
            /// </summary>
            public string? Landline { get; set; }
            /// <summary>
            /// 邮箱
            /// </summary>
            public string? Mailbox { get; set; }
            /// <summary>
            /// 客户联系人id
            /// </summary>
            public int CusContactId { get; set; }
            /// <summary>
            /// 备注
            /// </summary>
            public string? Cremark { get; set; }
            /// <summary>
            /// 联系人电话
            /// </summary>
            public string? Cphone { get; set; }
        }
    }
}
